/* GUI CONVERTED from glacbal1.i (converter v1.75) Thu Jan 25 08:09:33 2001 */
/* glacbal1.i - -- INCLUDE FILE TO CALCULATE BALANCE FOR ACCOUNT        */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                   */
/*                        EXCLUDES RETAINED EARNINGS FOR CURRENT YEAR   */
/* REVISION: 1.0      LAST MODIFIED: 10/10/86   BY: jms                 */
/* REVISION: 4.0      LAST MODIFIED: 09/02/88   BY: RML   *C0028*       */
/* REVISION: 5.0      LAST MODIFIED: 04/14/89   BY: JMS   *B066*        */
/*                                   01/22/90   by: jms   *B499*        */
/* REVISION: 6.0      LAST MODIFIED: 10/15/90   by: jms   *D034*        */
/*                                   01/23/91   by: jms   *D314*        */
/*                                   09/04/91   by: jms   *D849*        */
/* REVISION: 7.0      LAST MODIFIED: 11/18/91   by: jms   *F058*        */
/* REVISION: 7.3      LAST MODIFIED: 10/08/92   by: mpp   *G479*        */
/*                                   11/07/95   by: mys   *G1CL*        */
/* REVISION: 8.6      LAST MODIFIED: 06/17/96   by: jjp   *K001*        */
/* REVISION: 8.6      LAST MODIFIED: 03/18/98      BY: *J23W* Sachin Shah */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00      BY: *N0L1* Mark Brown  */
/* REVISION: 9.1      LAST MODIFIED: 09/18/00      BY: *N0VY* Mudit Mehta */
/************************************************************************/
/*!
This include file will calculate the balance for an account for financial
statements.  Year-end transactions to retained earnings for the current
fiscal year will be excluded.  Date range MAY NOT span fiscal years.
*/
/* REVISION: EB2 SP3  LAST MODIFIED: 11/27/03 BY: Kaine Zhang   *hk-s-023* */

/*K001* Added following comment */
           /*!
           glacbal4.i and glacbal5.i were copied from this program and
           modified and used in glabiqb.p and gldabrpa.p respectively
           Therefore if any modifications are made to this program then
           glacbal4.i and glacbal5.i should be checked to see if they
           need the same modifications.
           */

/*!
Parameters:
   {&acc}    account code
   {&sub}    sub-account code
   {&cc}     cost center
   {&begdt}  beginning date
   {&enddt}  ending date
   {&balance} balance
   {&yrend}   year end
   {&rptcurr} report currency
   {&accurr}  account currency
*/
/************************************************************************/
/*J23W* GROUPED MULTIPLE FIELD ASSIGNMENTS INTO ONE
        FOR PERFORMANCE AND SMALLER R-CODE */

/**************hk-s-023 begin add*************************
define shared temp-table temp_gglt no-undo
	field temp_acc		like	gltr_acc
	field temp_amt		like	gltr_amt
	field temp_ctr		like	gltr_ctr
	field temp_curramt	like	gltr_curramt
	field temp_ecur_amt	like	gltr_ecur_amt
	field temp_eff_dt	like	gltr_eff_dt
	field temp_entity	like	gltr_entity
	field temp_sub		like	gltr_sub
	field temp_tr_type	like	gltr_tr_type
	field temp_ref		like	gltr_ref
	field temp_line		like	gltr_line
	field temp_rflag	like	gltr_rflag.
**************hk-s-023 end add***************************/

/*N0VY*/ {cxcustom.i "GLACBAL1.I"}
          assign {&balance} = 0.
/*F058*/  if can-find(first acd_det where acd_acc = {&acc} and acd_sub = {&sub}
/*F058*/     and acd_cc = {&cc}) then do:

             assign dt = {&begdt}.

             /* CALCULATE PERIOD RANGES */
             if {&begdt} <> low_date then do:

/*J23W**         find first glc_cal where glc_cal.glc_start <= {&begdt} and **/
/*J23W**                  glc_cal.glc_end >= {&begdt} no-lock. **/

/*J23W*/        for first glc_cal fields ( glc_end glc_per glc_start glc_year)
/*J23W*/            where glc_cal.glc_start <= {&begdt} and
/*J23W*/             glc_cal.glc_end >= {&begdt} no-lock: end.

                /* IF BEGINNING DATE IS MID-PERIOD */
                if {&begdt} <> glc_cal.glc_start then do:
                   assign dt1 = glc_cal.glc_end.
                   if {&enddt} < dt1 then assign dt1 = {&enddt}.
/*******************hk-s-023 begin delete*************************
**                     for each en_mstr
**  /*J23W*/               fields (en_entity en_curr)
**                         where en_entity >= entity and
**                           en_entity <= entity1 no-lock,
**  /*J23W** make into join     for each gltr_hist **/
**  
**  /*J23W*/               each gltr_hist
**  /*J23W*/               fields (gltr_acc gltr_amt gltr_ctr gltr_curramt
**                    gltr_ecur_amt gltr_eff_dt gltr_entity
**                    gltr_sub gltr_tr_type)
**                         where gltr_entity = en_entity and
**                               gltr_acc = {&acc} and
**  /*F058*/                     gltr_sub = {&sub} and
**                               gltr_ctr = {&cc} and
**                               gltr_eff_dt >= {&begdt} and
**                               gltr_eff_dt <= dt1 no-lock
**  /*F058*/                     use-index gltr_sub:
**                              if {&rptcurr} = base_curr then do:
**                              assign
**                                 {&balance} = {&balance} + gltr_amt
**                                 knt = knt + 1.
**                              end.
**                              else if {&rptcurr} = {&accurr} then do:
**                  assign
**                                 {&balance} = {&balance} + gltr_curramt
**                                 knt = knt + 1.
**                              end.
**  /*G479*/                    else if {&rptcurr} = en_curr then do:
**                  assign
**  /*G479*/                        {&balance} = {&balance} + gltr_ecur_amt
**  /*G479*/                        knt = knt + 1.
**  /*G479*/                    end.
**  /*J23W**              end. **/
**                     end.
*********************hk-s-023 end delete********************/

/*******************hk-s-023 begin add*************************/
		for each en_mstr
			fields (en_entity en_curr)
			where en_entity >= entity and en_entity <= entity1 no-lock:
			for each temp_gglt
				fields (temp_acc temp_amt temp_ctr temp_curramt
					temp_ecur_amt temp_eff_dt temp_entity
					temp_sub temp_tr_type)
				where temp_entity = en_entity
					and temp_acc = {&acc}
					and temp_sub = {&sub}
					and temp_ctr = {&cc}
					and temp_eff_dt >= {&begdt}
					and temp_eff_dt <= dt1
			    NO-LOCK USE-INDEX temp_sub:
				if {&rptcurr} = base_curr then do:
					assign
						{&balance} = {&balance} + temp_amt
						knt = knt + 1.
				end.
				else if {&rptcurr} = {&accurr} then do:
					assign
						{&balance} = {&balance} + temp_curramt
						knt = knt + 1.
				end.
				else if {&rptcurr} = en_curr then do:
					assign
						{&balance} = {&balance} + temp_ecur_amt
						knt = knt + 1.
				end.
			end.
		end.
/*********************hk-s-023 end add********************/

                   assign dt = glc_cal.glc_end + 1.
                end.
/*N0VY*/     {&GLACBAL1-I-TAG1}
             end.
             /* READ PERIOD TOTALS */
/*J23W**     find first glc_cal where glc_cal.glc_end >= dt no-lock **/
/*J23W**     use-index glc_index no-error.                       **/

/*J23W*/    for first glc_cal fields ( glc_end glc_per glc_start glc_year)
/*J23W*/    where glc_cal.glc_end >= dt no-lock
/*J23W*/    use-index glc_index: end.
             repeat:
                if not available glc_cal then leave.
                if glc_cal.glc_start > {&enddt} or glc_cal.glc_end > {&enddt}
                   then leave.
                for each en_mstr
/*J23W*/          fields (en_entity en_curr)
                  where en_entity >= entity and
                                       en_entity <= entity1 no-lock,
/*J23W** make into join      for each acd_det  **/

/*J23W*/          each acd_det
/*J23W*/          fields (acd_acc acd_amt acd_cc acd_curr_amt acd_ecur_amt
              acd_entity acd_per acd_sub acd_year)
                  where acd_acc = {&acc} and
                        acd_cc = {&cc} and
/*F058*/                acd_sub = {&sub} and
                        acd_entity = en_entity and
                        acd_year = glc_cal.glc_year and
                        acd_per = glc_cal.glc_per no-lock
                        use-index acd_ind2:
                         if {&rptcurr} = base_curr then do:
                assign
                               {&balance} = {&balance} + acd_amt
                               knt = knt + 1.
                         end.
                         else if {&rptcurr} = {&accurr} then do:
                assign
                               {&balance} = {&balance} + acd_curr_amt
                               knt = knt + 1.
                         end.
/*G479*/                 else if {&rptcurr} = en_curr then do:
/*G479*/                       assign {&balance} = {&balance} + acd_ecur_amt
/*G479*/                       knt = knt + 1.
/*G479*/                 end.
/*J23W**           end. **/
                end.
                find next glc_cal no-lock no-error.
             end.

             /* ELIMINATE ANY YEAR-END CLOSE TRANSACTIONS FROM RET EARNINGS */
             if {&enddt} = {&yrend} and {&acc} = ret then do:

/************hk-s-023 begin delete***************
**           for each en_mstr
**  /*J23W*/              fields (en_entity en_curr )
**                        where en_entity >= entity and
**                        en_entity <= entity1 no-lock,
**  /*J23W** make into join      for each gltr_hist **/
**  
**  /*J23W*/              each gltr_hist
**  /*J23W*/              fields (gltr_acc gltr_amt gltr_ctr gltr_curramt
**                    gltr_ecur_amt gltr_eff_dt gltr_entity
**                    gltr_sub gltr_tr_type)
**  
**                     where gltr_entity = en_entity and
**                           gltr_acc = {&acc} and
**  /*F058*/                 gltr_sub = {&sub} and
**                           gltr_ctr = {&cc} and
**                           gltr_eff_dt = {&yrend} no-lock
**  /*F058*/                 use-index gltr_sub:
**                        if gltr_tr_type = "YR" or gltr_tr_type = "RA" then
**                           assign {&balance} = {&balance} - gltr_amt.
**  /*J23W**        end.**/
**                  end.
*******************hk-s-023 end delete*****************/

/************hk-s-023 begin add***************/
		for each en_mstr fields (en_entity en_curr )
			where en_entity >= entity and en_entity <= entity1 no-lock,
		each temp_gglt
			fields (temp_acc temp_amt temp_ctr temp_curramt
				temp_ecur_amt temp_eff_dt temp_entity
				temp_sub temp_tr_type)
			where temp_entity = en_entity
				and temp_acc = {&acc}
				and temp_sub = {&sub}
				and temp_ctr = {&cc}
				and temp_eff_dt = {&yrend}
		    NO-LOCK USE-INDEX temp_sub:
			if temp_tr_type = "YR" or temp_tr_type = "RA" then
				assign {&balance} = {&balance} - temp_amt.
		end.
/*******************hk-s-023 end add*******************/
             end.

/*N0VY*/     {&GLACBAL1-I-TAG2}
             /* READ ANY TRANSACTIONS FROM INCOMPLETE ENDING PERIOD */
             if available glc_cal then do:
                if glc_cal.glc_end <> {&enddt} then do:
                   assign dt1 = glc_cal.glc_start.
                   if {&begdt} > dt1 then assign dt1 = {&begdt}.
/*G1CL*/           if dt1 <= {&enddt} then do:

/***********hk-s-023 begin delete***************
**                       for each en_mstr
**  /*J23W*/                 fields (en_entity en_curr)
**                           where en_entity >= entity and
**                           en_entity <= entity1 no-lock,
**  /*J23W** make into join         for each gltr_hist  **/
**  
**  /*J23W*/                 each gltr_hist
**  /*J23W*/                 fields (gltr_acc gltr_amt gltr_ctr gltr_curramt
**                    gltr_ecur_amt gltr_eff_dt gltr_entity
**                    gltr_sub gltr_tr_type)
**                           where gltr_entity = en_entity and
**                                 gltr_acc = {&acc} and
**  /*F058*/                       gltr_sub = {&sub} and
**                                 gltr_ctr = {&cc} and
**                                 gltr_eff_dt >= dt1 and
**                                 gltr_eff_dt <= {&enddt} no-lock
**  /*F058*/                       use-index gltr_sub:
**                           if {&rptcurr} = base_curr then do:
**                              assign
**                              {&balance} = {&balance} + gltr_amt
**                              knt = knt + 1.
**                           end.
**                           else if {&rptcurr} = {&accurr} then do:
**                              assign
**                              {&balance} = {&balance} + gltr_curramt
**                              knt = knt + 1.
**                           end.
**  /*G479*/                 else if {&rptcurr} = en_curr then do:
**                      assign
**  /*G479*/                    {&balance} = {&balance} + gltr_ecur_amt
**  /*G479*/                    knt = knt + 1.
**  /*G479*/                 end.
**  /*J23W**              end. **/
**                     end.
************hk-s-023 end delete****************/

/***********hk-s-023 begin and***************/
			for each en_mstr fields (en_entity en_curr)
				where en_entity >= entity and
					en_entity <= entity1 no-lock,
			each temp_gglt
				fields (temp_acc temp_amt temp_ctr temp_curramt
					temp_ecur_amt temp_eff_dt temp_entity
					temp_sub temp_tr_type)
				where temp_entity = en_entity
					and temp_acc = {&acc}
					and temp_sub = {&sub}
					and temp_ctr = {&cc}
					and temp_eff_dt >= dt1
					and temp_eff_dt <= {&enddt}
			    NO-LOCK USE-INDEX temp_sub:
				if {&rptcurr} = base_curr then do:
					assign
						{&balance} = {&balance} + temp_amt
						knt = knt + 1.
				end.
				else if {&rptcurr} = {&accurr} then do:
					assign
						{&balance} = {&balance} + temp_curramt
						knt = knt + 1.
				end.
				else if {&rptcurr} = en_curr then do:
					assign
						{&balance} = {&balance} + temp_ecur_amt
						knt = knt + 1.
				end.
			end.
/************hk-s-023 end add****************/

/*G1CL*/           end. /*IF DT1 <= {&ENDDT}*/
                end.
             end.
/*F058*/  end.
